#include <iostream>
#include "BlockQueue.hpp"
#include "Thread.hpp"
#include "Task.hpp"
using namespace ThreadModule;
using Task = std::function<void()>;
using bock_queue_t = BlockQueue<Task>;
void printdata()
{
    std::cout << "hell word" << std::endl;
}
void producer(bock_queue_t &bq)
{
    while (true)
    {
        sleep(1);
        Task t = printdata;
        bq.enqueue(t);
    }
}
void consumer(bock_queue_t &bq)
{
    while (true)
    {
        Task tk;
        bq.pop(&tk); // 为什么传地址，通过地址修改cnt
        tk();
    }
}
void Start_Com(std::vector<Thread<BlockQueue<Task>>> *threads_ptr, bock_queue_t &bq, int num, func_t<bock_queue_t> func)
{
    for (int i = 0; i < num; i++)
    {
        std::string name = "thread-" + std::to_string(i + 1);
        threads_ptr->emplace_back(func, bq, name);
        threads_ptr->back().Start();
    }
}
void StartProducer(std::vector<Thread<bock_queue_t>> *threads_ptr, bock_queue_t &bq, int num)
{
    Start_Com(threads_ptr, bq, num, producer);
}
void StartConsumer(std::vector<Thread<bock_queue_t>> *threads_ptr, bock_queue_t &bq, int num)
{
    Start_Com(threads_ptr, bq, num, consumer);
}
void WaitAllThread(std::vector<Thread<bock_queue_t>> &threads)
{
    for (auto thread : threads)
    {
        thread.Join();
    }
}
int main()
{
    bock_queue_t *ptr = new bock_queue_t(5);
    std::vector<Thread<bock_queue_t>> threads;
    StartProducer(&threads, *ptr, 1);
    StartConsumer(&threads, *ptr, 3);
    WaitAllThread(threads);
    return 0;
}